<template>
  <view class="wrap">
    <uv-navbar :fixed="false" title="修改社群亮点" autoBack safeAreaInsetTop placeholder :border='false'>
      <template v-slot:left>
        <view class="navEditName nav-left">取消</view>
      </template>
      <template v-slot:right>
        <view class="navEditName nav-right" @click="tap">保存</view>
      </template>
    </uv-navbar>
    <view class="status"></view>
    <view class="list">
      <view class="item" v-for="(item, index) in list" :key='index'>
        <view class="editImage">
          <image :src="setUrl(item)" class="img"></image>
        </view>
        <image src="../../../static/delete.png" class="delete" @click="deleteImage(index)"></image>
      </view>
      <view class="item" @click="chooseImage">
        <view class="editImage">
          <image src="../../../static/upload.png" class="img"></image>
        </view>
      </view>
    </view>
    
    <action ref="actionPhoto" @click="getImageUrl"></action>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';
import { choose, getGroup, editGroup, toast, setUrl } from '@/common';
import { useOrder } from '@/store';

const actionPhoto = ref(null);
const store = useOrder();
const obj = uni.$uv.deepClone(store.group);
let list = ref(obj.poster_images ? obj.poster_images.split(',') : []);

function tap(){
  if(!list.value.length) return toast('请最少上传一张图片');
  const params = {
    id: obj.id,
    poster_images: list.value.join(',')
  }
  editGroup(params).then(res => {
    if(res.code == 1){
      toast('修改成功', true);
      getGroup(obj.id);
      setTimeout(() => {
        uni.navigateBack({
          delta: 1
        });
      }, 1000)
    }
  })
}
function chooseImage(){
  if(list.value.length >= 3) return toast('最多上传3张');
  
  actionPhoto.value.open();
}
function getImageUrl(url){
  list.value.push(url)
}
function deleteImage(index){
  list.value.splice(index, 1);
}
</script>

<style lang="scss" scoped>
  .list{
    padding: 30rpx;
    background-color: #fff;
    margin-top: 20rpx;
    display: flex;
    align-items: flex-start;
    flex-wrap: wrap;
    padding-top: 48rpx;
    .item{
      position: relative;
      width: 210rpx;
      height: 210rpx;
      margin-right: 28rpx;
      margin-bottom: 28rpx;
      &:nth-of-type(3n){
        margin-right: 0;
      }
      .delete{
        position: absolute;
        right: -20rpx;
        top: -20rpx;
        width: 40rpx;
        height: 40rpx;
      }
      .editImage{
        width: 210rpx;
        height: 210rpx;
        border-radius: 12rpx;
        overflow: hidden;
      }
    }
  }
  .navEditName{
    font-weight: 400;
    font-size: 28rpx;
    &.nav-left{
      color: #737373;
    }
    &.nav-right{
      color: #E43222;
    }
  }
  .status{
    width: 100%;
    height: 20rpx;
    background-color: #F5F5F5;
  }
  .wrap{
    width: 100%;
    min-height: 100vh;
    background-color: #fff;
  }
</style>